#include "HelloWorldScene.h"
#include <sys/types.h>

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    CCLOG("init");

    //////////////////////////////
    // 1. super init first
    if ( !LayerColor::initWithColor(Color4B(55, 66, 77, 255)))
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    Size mySize = this->getContentSize();
    
    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    
	closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);
    
    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    
    auto label = Label::createWithSystemFont("Hello World cocos2d-x!", "Arial", 28);
    label->setAlignment(TextHAlignment::CENTER);
    label->setVerticalAlignment(TextVAlignment::CENTER);
    label->setColor(Color3B::MAGENTA);
    // position the label on the center of the screen
    //label->setPosition(Vec2(visibleSize.width/2, visibleSize.height - label->getContentSize().height));
    label->ignoreAnchorPointForPosition(true);
    Size labelSize = label->getContentSize();
    label->setPosition(Vec2(0, mySize.height - labelSize.height));
    label->setWidth(mySize.width);
    labelSize.width = mySize.width;
    //label->setContentSize(labelSize);
    // add the label as a child to this layer
    this->addChild(label);

    if (0) {
    // add "HelloWorld" splash screen"
    auto sprite = Sprite::create("HelloWorld.png");
    auto spriteSize = sprite->getContentSize();

    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    sprite->setScale((float)visibleSize.width / spriteSize.width, (float)visibleSize.height / spriteSize.height);
    
    // add the sprite as a child to this layer
    this->addChild(sprite, 0);
    }
    
    _sprite = Sprite::create("Icon-50.png");
    auto spriteSize = _sprite->getContentSize();
    // position the sprite on the center of the screen
    _sprite->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
    
    // add the sprite as a child to this layer
    this->addChild(_sprite, 0);
    
    this->schedule(schedule_selector(HelloWorld::updateGame));

#if 1
    auto btn = ControlButton::create();
    btn->setAnchorPoint(Vec2(0,0));
    auto frame = SpriteFrame::create("Icon-50.png", Rect(0, 0, spriteSize.width, spriteSize.height));
    btn->setBackgroundSpriteFrameForState(frame, Control::State::NORMAL);
    btn->setContentSize(spriteSize);
    btn->setPreferredSize(spriteSize);
    btn->addTargetWithActionForControlEvents(this, (Control::Handler)&HelloWorld::ControlHandler, Control::EventType::TOUCH_UP_INSIDE);
    this->addChild(btn);
    btn->setPosition(Vec2(visibleSize.width/2 + origin.x, 0));
#endif

    if (1)
    {
        auto btn2 = ControlButton::create("0", "Arial", 48);
        btn2->setTag(0);
        btn2->setZoomOnTouchDown(false);
        btn2->setAnchorPoint(Point::ZERO);
        btn2->setTitleColorForState(Color3B::WHITE, Control::State::NORMAL);
        btn2->setPosition(20, 200);
        this->addChild(btn2);

        btn2->setTitleForState("x", Control::State::NORMAL);
    }
    
    CCLOG("main thread: %ld\n", (intptr_t)pthread_self());
    return true;
}

void HelloWorld::ControlHandler(Ref* sender, Control::EventType eventType)
{
    CCLOG("ControlHandler: %d", eventType);
    
}

void HelloWorld::updateGame(float dt)
{
    //CCLOG("updateGame: delta = %f", dt);
    //CCLOG("updateGame thread: %ld\n", (intptr_t)pthread_self());
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    //CCLOG("Acceleration: %f, %f, %f @%f", _accel.x, _accel.y, _accel.z, _accel.timestamp);

    Vec2 pos = _sprite->getPosition();
    //CCLOG("Old pos: %f, %f", pos.x, pos.y);

    if (_accel.x != 0 && _accel.y != 0)
    {
        pos.x += _accel.x * 50;
        if (pos.x < origin.x) pos.x = origin.x;
        if (pos.x > origin.x + visibleSize.width) pos.x = origin.x + visibleSize.width;
        pos.y += _accel.y * 45;
        if (pos.y < origin.y) pos.y = origin.y;
        if (pos.y > origin.y + visibleSize.height) pos.y = origin.y + visibleSize.height;
        //CCLOG("New pos: %f, %f", pos.x, pos.y);
    
        _sprite->setPosition(pos);
    }

}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

void HelloWorld::accelerationHandler(cocos2d::Acceleration* accel, cocos2d::Event* event)
{
    //CCLOG("accelerationHandler thread: %ld\n", (intptr_t)pthread_self());
    //CCLOG("Acceleration: %f, %f, %f @%f\n", accel->x, accel->y, accel->z, accel->timestamp);
    
    _accel = *accel;
}


void HelloWorld::onEnter()
{
    CCLOG("onEnter");
    Layer::onEnter();
    
    _accelListener = EventListenerAcceleration::create(CC_CALLBACK_2(HelloWorld::accelerationHandler, this));
    CCLOG("EventListenerAcceleration::isEnabled: %d\n", (int)_accelListener->isEnabled());
    this->_eventDispatcher->addEventListenerWithSceneGraphPriority(_accelListener, this);
}

void HelloWorld::onExit()
{
    CCLOG("onExit");
    Layer::onExit();
    
    this->_eventDispatcher->removeEventListener(_accelListener);
}


